package presentation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.RectangleInsets;

import control.CControl;

public class PCar extends JPanel implements ComponentListener, Observer {
	private static final long serialVersionUID = 1L;
	private JPanel centerPanel, bottomPanel1, bottomPanel2;
	private ArrayList<ChartPanel> chartPanels = new ArrayList<ChartPanel>();
	
	public PCar(CControl cControl) {
		this.addComponentListener(this);
		this.setLayout(new BorderLayout());
		cControl.addStreamObserver(this);
		
		centerPanel = new PCarPanel(cControl);
		centerPanel.setBackground(Color.white);
		this.add(centerPanel,BorderLayout.PAGE_START);
		
		JPanel spacing1 = new JPanel();
		spacing1.setPreferredSize(new Dimension(20,20));
		spacing1.setBackground(Color.white);
		
		JPanel spacing2 = new JPanel();
		spacing2.setPreferredSize(new Dimension(20,20));
		spacing2.setBackground(Color.white);
		
		bottomPanel1 = new JPanel();
		bottomPanel1.setBackground(Color.white);
		//addPercentMeter("TPS", "Throttle", cControl, bottomPanel1);
		//addPercentMeter("ClutchPosition", "Clutch", cControl, bottomPanel1);
		addPressureDial("FSN_Brake1", "Brake cylinder 1", cControl, bottomPanel1);
		addPressureDial("FSN_Brake2", "Brake cylinder 2", cControl, bottomPanel1);
		
		//addPressureDial("MAP", "Manifold Air Pressure", cControl, bottomPanel1,1000.);
		this.add(bottomPanel1,BorderLayout.CENTER);
		
		bottomPanel2 = new JPanel();
		bottomPanel2.setBackground(Color.white);
		//addThermometer("ECT","Engine Cooleant Temp",cControl,bottomPanel2);
		//addThermometer("IAT","Intake Air Temp",cControl,bottomPanel2);
		addThermometer("RSN_OilTemp1","Oil Temp 1",cControl,bottomPanel2);
		addThermometer("RSN_OilTemp2","Oil Temp 2",cControl,bottomPanel2);
		addThermometer("RSN_OilTemp3","Oil Temp 3",cControl,bottomPanel2);
		addThermometer("RSN_OilTemp4","Oil Temp 4",cControl,bottomPanel2);
		//bottomPanel2.add(spacing1);
		//addPressureDial("OilPressure", "Oil pressure", cControl, bottomPanel2);
		//bottomPanel2.add(spacing2);
		//addPressureDial("FuelPressure", "Fuel pressure", cControl, bottomPanel2);
		this.add(bottomPanel2,BorderLayout.PAGE_END);
	}
	
	private void addPercentMeter(String register, String name, CControl cControl, JPanel parent) {
		double[] boundary = cControl.getRegisterBoundaries(register);
		String unit = cControl.getRegisterUnit(register);
		PPercentMeter pressure = new PPercentMeter(unit,boundary);
		cControl.addRegisterObserver(register, pressure);
		JFreeChart pressureChart = new JFreeChart(name,pressure);
		pressureChart.setPadding(new RectangleInsets(0,25,20,25));
		pressureChart.setBackgroundPaint(Color.white);
		ChartPanel pressurePanel = new ChartPanel(pressureChart);
		chartPanels.add(pressurePanel);
		parent.add(pressurePanel);
	}
	
	private void addThermometer(String register, String name, CControl cControl, JPanel parent) {
		double[] boundary = cControl.getRegisterBoundaries(register);
		PThermometer temp = new PThermometer(boundary);
		cControl.addRegisterObserver(register, temp);
		JFreeChart tempChart = new JFreeChart(name,temp);
		tempChart.setBackgroundPaint(Color.white);
		ChartPanel tempPanel = new ChartPanel(tempChart);
		chartPanels.add(tempPanel);
		parent.add(tempPanel);
	}
	
	private void addPressureDial(String register, String name, CControl cControl, JPanel parent) {
		double[] boundary = cControl.getRegisterBoundaries(register);
		String unit = cControl.getRegisterUnit(register);
		PPressureDial pressure = new PPressureDial(unit,boundary,1.);
		cControl.addRegisterObserver(register, pressure);
		JFreeChart pressureChart = new JFreeChart(name,pressure);
		pressureChart.setPadding(new RectangleInsets(0,25,20,25));
		pressureChart.setBackgroundPaint(Color.white);
		ChartPanel pressurePanel = new ChartPanel(pressureChart);
		chartPanels.add(pressurePanel);
		parent.add(pressurePanel);
	}
	
	private void addPressureDial(String register, String name, 
			CControl cControl, JPanel parent, double multiplier) {
		double[] boundary = cControl.getRegisterBoundaries(register);
		boundary[0] = boundary[0]*multiplier;
		boundary[1] = boundary[1]*multiplier;
		String unit = "m" + cControl.getRegisterUnit(register);
		PPressureDial pressure = new PPressureDial(unit,boundary,multiplier);
		cControl.addRegisterObserver(register, pressure);
		JFreeChart pressureChart = new JFreeChart(name,pressure);
		pressureChart.setPadding(new RectangleInsets(0,25,20,25));
		pressureChart.setBackgroundPaint(Color.white);
		ChartPanel pressurePanel = new ChartPanel(pressureChart);
		chartPanels.add(pressurePanel);
		parent.add(pressurePanel);
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {}

	@Override
	public void componentMoved(ComponentEvent arg0) {}

	@Override
	public void componentResized(ComponentEvent arg0) {
		int panelWidth = this.getWidth();
		Double centerHeight = this.getHeight()*0.50;
		Double bottomHeight = this.getHeight()*0.25;
		
		int dialHeight = bottomHeight.intValue()-bottomHeight.intValue()/8;

		centerPanel.setPreferredSize(new Dimension(panelWidth,centerHeight.intValue()));
		bottomPanel1.setPreferredSize(new Dimension(panelWidth,bottomHeight.intValue()));
		bottomPanel2.setPreferredSize(new Dimension(panelWidth,bottomHeight.intValue()));
		
		for (int i = 0; i < chartPanels.size(); i++) {
			chartPanels.get(i).setPreferredSize(new Dimension(dialHeight,dialHeight));
		}
		
		this.updateUI();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {}

	@Override
	public void update(Observable arg0, Object arg1) {
		centerPanel.updateUI();		
	}
}
